#
#  Generated from genmsg/cmake/genmsg-extras.cmake.in
#
if (NOT genmsg_SOURCE_DIR)
  file(GLOB CATKIN_INSTALLED_GENLANGS
    RELATIVE @CMAKE_INSTALL_PREFIX@/etc/langs
    @CMAKE_INSTALL_PREFIX@/etc/langs/gen*)
  message(STATUS "Found these generator languages from the installation: ${CATKIN_INSTALLED_GENLANGS}")
  message(STATUS "Found these generator languages in the workspace: ${CATKIN_GENLANGS}")
  foreach(lang ${CATKIN_INSTALLED_GENLANGS})
    find_package(${lang} REQUIRED)
    list(FIND CATKIN_GENLANGS ${lang} _index)
    if(_index EQUAL -1)
      list(APPEND CATKIN_GENLANGS ${lang})
    endif()
  endforeach()
  message(STATUS "Using these generator languages: ${CATKIN_GENLANGS}")
endif()

macro(_prepend_path ARG_PATH ARG_FILES ARG_OUTPUT_VAR)
  # todo, check for proper path, slasheds, etc
  set(${ARG_OUTPUT_VAR} "")
  foreach(file ${ARG_FILES})
    list(APPEND ${ARG_OUTPUT_VAR} ${ARG_PATH}/${file})
  endforeach()
endmacro()

macro(add_message_files)
  parse_arguments(ARG "PACKAGE;FILES;DIRECTORY" "NOINSTALL;PRIVATE" ${ARGN})
  check_unused_arguments("add_message_files" "${ARG_DEFAULT_ARGS}")

  if(NOT ARG_PACKAGE)
    set(ARG_PACKAGE ${PROJECT_NAME})
  endif()

  if(CMAKE_HOST_UNIX)
    string(REGEX MATCH "^/.*" IS_FULL_PATH ${ARG_DIRECTORY})
  else() # Windows
    string(REGEX MATCH "^.:.*" IS_FULL_PATH ${ARG_DIRECTORY})
  endif()

  if(IS_FULL_PATH)
    set(DIRECTORY_FULL_PATH ${ARG_DIRECTORY})
  else()
    set(DIRECTORY_FULL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_DIRECTORY})
  endif()

  #todo, hack: handle this elsewhere when fixing the install stuff
  if(NOT ARG_PRIVATE)
    list(APPEND ${PROJECT_NAME}_MSG_INCLUDE_DIRS ${DIRECTORY_FULL_PATH})
  endif()

  _prepend_path(${DIRECTORY_FULL_PATH} "${ARG_FILES}" FILES_W_PATH)

  list(APPEND ${PROJECT_NAME}_MESSAGE_FILES ${FILES_W_PATH})
  list(APPEND ${PROJECT_NAME}_MESSAGE_FILES_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_DIRECTORY})

  if(NOT ARG_NOINSTALL)
    install(FILES ${FILES_W_PATH} DESTINATION share/${PROJECT_NAME}/${ARG_DIRECTORY})
  endif()
endmacro()

macro(add_service_files)
  parse_arguments(ARG "PACKAGE;FILES;DIRECTORY" "NOINSTALL" ${ARGN})
  check_unused_arguments("add_service_files" "${ARG_DEFAULT_ARGS}")

  if(NOT ARG_PACKAGE)
    set(ARG_PACKAGE ${PROJECT_NAME})
  endif()

  if(CMAKE_HOST_UNIX)
    string(REGEX MATCH "^/.*" IS_FULL_PATH ${ARG_DIRECTORY})
  else() # Windows
    string(REGEX MATCH "^.:.*" IS_FULL_PATH ${ARG_DIRECTORY})
  endif()

  if(IS_FULL_PATH)
    set(DIRECTORY_FULL_PATH ${ARG_DIRECTORY})
  else()
    set(DIRECTORY_FULL_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_DIRECTORY})
  endif()

  _prepend_path(${DIRECTORY_FULL_PATH} "${ARG_FILES}" FILES_W_PATH)

  list(APPEND ${PROJECT_NAME}_SERVICE_FILES ${FILES_W_PATH})

  if(NOT ARG_NOINSTALL)
    install(FILES ${FILES_W_PATH} DESTINATION share/${PROJECT_NAME}/${ARG_DIRECTORY})
  endif()
endmacro()

macro(generate_messages)
  parse_arguments(ARG "DEPENDENCIES;LANGS" "" ${ARGN})
  check_unused_arguments("generate_messages" "${ARG_DEFAULT_ARGS}")

  set(ARG_MESSAGES ${${PROJECT_NAME}_MESSAGE_FILES})
  set(ARG_SERVICES ${${PROJECT_NAME}_SERVICE_FILES})
  set(ARG_DEPENDENCIES ${ARG_DEPENDENCIES})

  if(ARG_LANGS)
    set(GEN_LANGS ${ARG_LANGS})
  else()
    set(GEN_LANGS ${CATKIN_GENLANGS})
  endif()

  set(MSG_SEARCH_DIRS "${PROJECT_NAME}")
  list(APPEND MSG_SEARCH_DIRS "${MESSAGE_FILES_DIR}")
  foreach(dep ${PROJECT_NAME} ${ARG_DEPENDENCIES})
    if(NOT ${dep}_FOUND AND NOT ${dep}_SOURCE_DIR)
      message(FATAL_ERROR "Messages depends on unknown pkg: ${dep} (Missing find_package(${dep}?)")
    endif()

    if(NOT ${dep}_SOURCE_DIR)
        set(${dep}_MSG_INCLUDE_DIRS ${${dep}_DIR}/../msg)
    endif()

    foreach(i ${${dep}_MSG_INCLUDE_DIRS})
      list(APPEND MSG_SEARCH_DIRS "${dep}")
      list(APPEND MSG_SEARCH_DIRS "${i}")
    endforeach()
  endforeach()
  set(${PROJECT_NAME}_MSG_INCLUDE_DIRS "${${PROJECT_NAME}_MSG_INCLUDE_DIRS}"
    CACHE FILEPATH "dir containing messages in @PACKAGE_NAME@")

  # mark that generate_messages() was called in order to detect wrong order of calling with catkin_python_setup()
  set(${PROJECT_NAME}_GENERATE_MESSAGES TRUE)
  # check if catkin_python_setup() was called in order to disable installation of gen/py stuff
  set(SKIP_INSTALL_GEN_PY ${${PROJECT_NAME}_CATKIN_PYTHON_SETUP})

  em_expand(@PKG_CMAKE_SRC_DIR@/pkg-genmsg.context.in
    ${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}-genmsg-context.py
    @PKG_CMAKE_SRC_DIR@/pkg-genmsg.cmake.em
    ${CMAKE_CURRENT_BINARY_DIR}/cmake/${PROJECT_NAME}-genmsg.cmake
    )
endmacro()

#
#  GENERATOR_TAG is lowercase alpha, like cpp, py, pybindings, typelibxml
#  GENERATOR_INSTALL_DIR is relative to CMAKE_INSTALL_PREFIX
#
macro(generate_generator GENERATOR_TAG GENERATOR_INSTALL_DIR GENERATOR_PRETTY GENERATOR_FILEEXT)
  set(GENERATOR_PRETTY ${GENERATOR_PRETTY})
  set(GENERATOR_FILEEXT ${GENERATOR_FILEEXT})
  if (genmsg_SOURCE_DIR)
    set(template ${genmsg_SOURCE_DIR}/cmake/generator.cmake.in)
  else()
    set(template @CMAKE_INSTALL_PREFIX@/share/genmsg/cmake/generator.cmake.in)
  endif()

  set(GENERATOR ${GENERATOR_TAG})
  set(expanded ${CMAKE_CURRENT_BINARY_DIR}/cmake/gen${GENERATOR}.cmake)

  configure_file(${template} ${expanded} @ONLY)

  include(${expanded})
  install(FILES ${expanded} DESTINATION share/${PROJECT_NAME}/cmake)
endmacro()
